package com.jic.product.impl;

import java.lang.Long;
import java.util.List;

import com.jic.product.entity.ProductPrice;
import com.jic.product.entity.ProductPriceAudit;
import com.jic.product.mapper.ProductPriceAuditMapper;
import com.jic.product.mapper.ProductPriceMapper;
import com.jic.product.manage.response.ProductPriceAuditResponse;
import com.jic.product.service.ProductPriceAuditService;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品价格变更审核表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-01-10 07:03:01
 */
@Service
public class ProductPriceAuditServiceImpl implements ProductPriceAuditService {

    @Autowired
    private ProductPriceAuditMapper productPriceAuditMapper;
    @Autowired
    private ProductPriceMapper productPriceMapper;

    @Override
    public long insert(ProductPriceAudit productPriceAudit) {
        long flag = productPriceAuditMapper.insertProductPriceAudit(productPriceAudit);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return productPriceAuditMapper.deleteProductPriceAuditById(id);
    }

    @Override
    public long update(ProductPriceAudit productPriceAudit) {
        //审核通过 修改价格表价格
        if (productPriceAudit.getStatus() == 1) {
            ProductPriceAudit productPriceAudit1 = this.queryByPrimaryKey(productPriceAudit.getId());
            ProductPrice productPrice = new ProductPrice();
            productPrice.setSalePriceWithTax(productPriceAudit1.getAfterValue());
            productPrice.setProductId(productPriceAudit1.getProductId());
            productPriceMapper.updateProductDistributeStore(productPrice);
        }
        return productPriceAuditMapper.updateProductPriceAudit(productPriceAudit);
    }

    @Override
    public ProductPriceAudit queryByPrimaryKey(Long id) {
        return productPriceAuditMapper.selectProductPriceAuditById(id);
    }

    @Override
    public List<ProductPriceAudit> query(ProductPriceAudit productPriceAudit) {
        return productPriceAuditMapper.selectProductPriceAuditList(productPriceAudit);
    }

    @Override
    public PageResult<ProductPriceAuditResponse> queryPage(ProductPriceAudit productPriceAudit, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductPriceAuditResponse> list = productPriceAuditMapper.queryPage(startIndex, pageQuery.getLimit(), productPriceAudit);
        long count = productPriceAuditMapper.queryPageCount(productPriceAudit);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}